package com.hibase.core.rule.service.custom.impl;

import cn.hutool.core.util.ArrayUtil;
import com.hibase.core.rule.mapper.custom.CustomMapper;
import com.hibase.core.rule.service.custom.CustomSerive;
import com.hibase.common.exception.HibaseAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义sql service接口
 *
 * @author hufeng
 * @date 2019/04/18
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CustomSeriveImpl implements CustomSerive {

    @Autowired
    private CustomMapper customMapper;

    @Override
    public List<Map<String, Object>> select(Object... values) {

        boolean checkValues = ArrayUtil.isNotEmpty(values);

        HibaseAssert.isTrue(checkValues, "参数不合法，缺少sql语句");

        Map<String, Object> map = new HashMap<>();

        if (ArrayUtil.isNotEmpty(values)) {

            int index = 0;

            for (Object object : values) {

                if (index == 0) {

                    map.put(CustomMapper.PARAM_SQL_TEXT, object);
                } else {

                    map.put(index + "", object);
                }

                index++;
            }
        }

        return customMapper.select(map);
    }
}
